function model = toaster_model_init (delta_t, T_0, T_a)
%
% model = toaster_model_init (delta_t, T_0, T_a)  
%
% Initialise the toaster model, where:  
%  delta_t is the sampling interval
%  T_0     is the initial temperature
%  T_a     is the ambient temperature
%  model   is a model structure
%
% See also toaster_model_temp and toaster_model_update.

% File:        toaster_model_init.m
% Author:      M. P. Hayes UC ECE
% Date:        1 October 2007

  % Duration of impulse response (s).
  T = 4000;
  
  % Heater heat storage.
  C_h = 2.82;
  % Bar heat storage.
  C_b = 21.6;
  % Convection resistance (bar to ambient).
  R_ba = 15.4;
  % Conduction resistance (heater to bar).
  R_hb = 5.32;
  % Time delay.
  t_d = 14.6;
  
  Ns = T / delta_t;
    
  model.t_d = t_d;
  model.delta_t = delta_t;
  model.T = T_0;
  model.T_a = T_a;
  model.n = 0;

  % Heater power history.
  model.p = zeros (Ns, 1);
  
  % Calculate the numerator and denominator of the transfer function.
  model.HN = [1] / (C_h * C_b * R_hb);
  model.HD = [1, 1.0 / (C_b * R_ba) + 1.0 / (C_b * R_hb) ...
        + 1.0 / (C_h * R_hb), 1.0 / (C_h * R_hb * C_b * R_ba)].';

  % Calculate the numerator and denominator of the transient
  % response due to the initial conditions.
  model.IN = T_0 * [1, 1.0 / (C_b * R_hb) + 1.0 / (C_h * R_hb), 0] ...
      + T_a * [0, 1.0 / (C_b * R_ba), 1.0 / (C_b * C_h * R_hb * R_ba)];
  model.ID = conv (model.HD, [1, 0]);
  
  % Note when T_a = T_0 the transient response due to the initial
  % conditions simplifies to T_a u(t).
  
  % Calculate the impulse response.
  t = [0 : Ns - 1].' * delta_t;

  model.h = mimpresp (model.HN, model.HD, t - model.t_d);
  
  % Calculate the transient response due to the initial conditions.  
  model.i = mimpresp (model.IN, model.ID, t);
